<template>
  <div class="header">
    <div class="header__title">
      <van-icon :name="statusIconName" color="#fff" size="25" />
      <span class="header__title__text">{{ statusText }}</span>
    </div>

    <p v-if="status === 'pending'" class="header__desc">
      支付处理中，请稍候...
    </p>
    <p v-if="isSuccess" class="header__desc">我们将尽快为您配货~</p>
    <p v-if="!isSuccess && status !== 'pending'" class="header__desc">
      请联系客服~
    </p>

    <div class="header__btn">
      <router-link
        class="header__btn__item"
        to="/"
        replace
      >返回首页</router-link>
      <!-- <router-link
        class="header__btn__item"
        :to="'/cart'"
        replace
      >查看购物车</router-link> -->
      <router-link
        class="header__btn__item"
        replace
        :to="to"
      >查看订单</router-link>
    </div>
  </div>
</template>

<script>
import { payStatus } from '@/api/pay'

export default {
  name: 'PayStatus',

  props: {
    status: {
      type: String,
      default: 'pending' // 默认值设为'pending'以支持新状态
    },
    orderType: {
      type: String,
      default: ''
    }
    // orderNo: {
    //   type: String,
    //   default: "",
    // },
  },
  data() {
    return {
      // isSuccess 不再直接在 data 中定义，而通过 computed 依赖于 status
      orderNo: sessionStorage.getItem('orderSn'),
      to: sessionStorage.getItem('payRedirct') || '',
      type: sessionStorage.getItem('orderType'),
      setIntervalIndex: 0,
      index: 0
    }
  },
  computed: {
    statusText() {
      return (
        {
          success: '支付成功',
          failed: '支付失败',
          pending: '支付中'
        }[this.status] || '未知状态'
      )
    },
    statusIconName() {
      return (
        {
          success: 'checked',
          failed: 'close',
          pending: 'loading'
        }[this.status] || 'checked'
      )
    },
    orderStatus() {
      return this.status === 'success' ? 2 : this.status === 'failed' ? 0 : 1
    },
    isSuccess() {
      return this.status === 'success'
    }
  },
  async mounted() {
    await this.getCode()
    // if(res.code === 200){

    // }
    // if (this.to !== '') {
    //   if (this.status !== 'success') {
    //     this.to = ''
    //   } else {
    //     this.to = sessionStorage.getItem('payRedirct')
    //   }
    // } else {
    //   this.to = `/user/order/list/${this.orderStatus}`
    // }
    // if (this.status === 'pending') {
    //   // 初始化轮询计数器
    //   this.pollingCount = 0
    //   this.pollingTimer = setInterval(() => this.checkPaymentStatus(), 3000) // 每3秒查询一次
    // }
    // zpy
    // this.status = "success";
  },

  beforeDestroy() {
    // 在组件销毁前清除定时器
    if (this.pollingTimer) {
      clearInterval(this.pollingTimer)
    }
  },

  methods: {
    async getCode() {
      if (this.index) {
        clearTimeout(this.index)
      }
      const res = await this.checkPaymentStatus()
      console.log(res)
      const status = res.map?.status
      if (status === 101 || status === 1) {
        this.status = 'pending'
        this.to = this.to + "1"
        this.index = setTimeout(() => {
          console.log(this.setIntervalIndex)
          clearTimeout(this.index)
          this.setIntervalIndex = this.setIntervalIndex + 1
          if (this.setIntervalIndex >= 3) {
            return false
          }
          this.getCode()
        }, 3000)
      } else if (status === 102 || status === 103 || status === 2) {
        this.status = 'failed'
        this.to + 1
      } else if (this.status === 201 || status === 3) {
        this.to + 2
        this.status = 'success'
      }
    },
    showOrder() {
      this.$router.push(this.to)
    },
    checkPaymentStatus() {
      const data = 'orderNo=' + this.orderNo + '&type=' + this.type
      // payStatus({ orderNo: this.orderNo })
      return payStatus(data)
      // .then((res) => {
      //   const newStatus = res.map.result
      //   if (newStatus === 'success') {
      //     clearInterval(this.pollingTimer)
      //     this.status = 'success'
      //     this.$toast.success('支付成功')
      //   } else if (newStatus === 'failed') {
      //     clearInterval(this.pollingTimer)
      //     this.status = 'failed'
      //     this.$toast.fail('支付失败，请重试')
      //   }
      //   // 如果状态仍然是 pending，则继续轮询
      //   if (newStatus === 'pending') {
      //     this.pollingCount++
      //     if (this.pollingCount >= 4) {
      //       // 达到10秒（共4次，每次3秒）
      //       this.$toast.info('支付结果查询超时，请稍后查看订单状态')
      //       clearInterval(this.pollingTimer)
      //     }
      //   }
      // })
      // .catch((err) => {
      //   console.log(err)
      //   // this.$dialog.alert({ message: '支付失败' })
      //   this.$router.replace({
      //     name: 'PayStatus',
      //     params: {
      //       status: 'failed'
      //     }
      //   })
      // })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.header {
  width: 100%;
  height: 390px;
  background: $red;
  .header__title {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    padding-top: 92px;
    .header__title__text {
      font-size: 42px;
      font-weight: 500;
      color: #fff;
      margin-left: 12px;
    }
  }
  .header__desc {
    font-size: 26px;
    color: #fff;
    margin-top: 27px;
    text-align: center;
    opacity: 0.9;
  }
  .header__btn {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    margin-top: 48px;
    .header__btn__item {
      box-sizing: border-box;
      width: 180px;
      height: 60px;
      border-radius: 33px;
      border: 1px solid#fff;
      line-height: 60px;
      text-align: center;
      font-size: 26px;
      color: #fff;
      &:last-child {
        margin-left: 60px;
      }
    }
  }
}
</style>
